#!/bin/bash
#
# $Id$
#

users=$(getent passwd | cut -d: -f1)

for user in $users 
do
    gid=$(getent passwd $user | cut -d: -f4)
    #primary group
        prgroup=$(getent group $gid | cut -d: -f1)
    #other group
        usergrps=$(groups $user | cut -d: -f2 | sed 's/^ //; s/ *'$prgroup' *//; s/ /,/g')
    shadow_line=$(sudo cat /etc/shadow | grep ^$user:)
    #state
        state="active"
        [ ! -z "$(echo $shadow_line | awk -F: '$2~/*/ {print}')" ] && state="not available"
        [ ! -z "$(echo $shadow_line | awk -F: '$2~/!/ {print}')" ] && state="locked"
    #password expiration
        current_date=$(perl -e 'printf("%i\n", time/86400);')
        change_date=$(echo $shadow_line | cut -d: -f3)
        valid_days=$(echo $shadow_line | cut -d: -f5)
        if [ -z "$valid_days" -o $valid_days -eq 99999 ]; then
            remain_days=99999
        else
            remain_days=$(expr $change_date + $valid_days - $current_date)
        fi
    echo '  <user username="'$user'">'
    echo '   <prgroup>'$prgroup'</prgroup>'
    echo '   <usergroups>'$usergrps'</usergroups>'
    echo '   <state>'$state'</state>'
    echo '   <remain_days>'$remain_days'<remain_days>'
    echo '  </user>'
done

